package cn.ibizlab.odoo.core.odoo_repair.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Irepair_line;
import cn.ibizlab.odoo.core.odoo_repair.domain.Repair_line;
import cn.ibizlab.odoo.core.odoo_repair.filter.Repair_lineSearchContext;
import cn.ibizlab.odoo.core.odoo_repair.service.IRepair_lineService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[修理明细行(零件)] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Repair_lineServiceImpl implements IRepair_lineService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Irepair_lineClientService repair_lineClientService;

    @Override
    public Repair_line get(Integer id) {
        Irepair_line clientModel = repair_lineClientService.createModel();
        clientModel.setId(id);
        repair_lineClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Repair_line get(Repair_line repair_line) {
        Repair_line retrepair_line = this.get(repair_line.getId()) ;
    	BeanCopier copier=BeanCopier.create(retrepair_line.getClass(), repair_line.getClass(), false);
        copier.copy(retrepair_line, repair_line, null);
        return repair_line ;
    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //repair_lineClientService.removeBatch(repair_lines) ;

    }

    @Override
    public boolean update(Repair_line repair_line) {
        boolean bOk = false ;
        Irepair_line clientModel = this.convert2Model(repair_line,null);
        repair_lineClientService.update(clientModel) ;
        this.convert2Domain(clientModel,repair_line) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public void createBatch(List<Repair_line> repair_lines, int batchSize) {

                        //repair_lineClientService.createBatch(repair_lines,batchSize) ;

    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Irepair_line clientModel = repair_lineClientService.createModel();
        clientModel.setId(id);
        repair_lineClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public void updateBatch(List<Repair_line> repair_lines, int batchSize) {

        //repair_lineClientService.updateBatch(repair_lines,batchSize) ;

    }

    @Override
    public boolean create(Repair_line repair_line) {
        boolean bOk = false ;
        Irepair_line clientModel = this.convert2Model(repair_line,null);
        repair_lineClientService.create(clientModel) ;
        this.convert2Domain(clientModel,repair_line);
        bOk = true ;

        return bOk ;
    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Repair_line> searchDefault(Repair_lineSearchContext context) {
        List<Repair_line> list = new ArrayList<Repair_line>();
        Page<cn.ibizlab.odoo.core.client.model.Irepair_line> clientModelList = repair_lineClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Irepair_line clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Repair_line>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据发票行查询
     */
    @Override
    public Page<Repair_line> selectByOdoo_invoice_line(Integer id){
        Repair_lineSearchContext context = new Repair_lineSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Repair_line> list = new ArrayList<Repair_line>();
        Page<cn.ibizlab.odoo.core.client.model.Irepair_line> clientModelList = repair_lineClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Irepair_line clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Repair_line>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据产品查询
     */
    @Override
    public Page<Repair_line> selectByOdoo_product(Integer id){
        Repair_lineSearchContext context = new Repair_lineSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Repair_line> list = new ArrayList<Repair_line>();
        Page<cn.ibizlab.odoo.core.client.model.Irepair_line> clientModelList = repair_lineClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Irepair_line clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Repair_line>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据维修单查询
     */
    @Override
    public Page<Repair_line> selectByOdoo_repair(Integer id){
        Repair_lineSearchContext context = new Repair_lineSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Repair_line> list = new ArrayList<Repair_line>();
        Page<cn.ibizlab.odoo.core.client.model.Irepair_line> clientModelList = repair_lineClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Irepair_line clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Repair_line>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Repair_line> selectByOdoo_create(Integer id){
        Repair_lineSearchContext context = new Repair_lineSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Repair_line> list = new ArrayList<Repair_line>();
        Page<cn.ibizlab.odoo.core.client.model.Irepair_line> clientModelList = repair_lineClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Irepair_line clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Repair_line>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Repair_line> selectByOdoo_write(Integer id){
        Repair_lineSearchContext context = new Repair_lineSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Repair_line> list = new ArrayList<Repair_line>();
        Page<cn.ibizlab.odoo.core.client.model.Irepair_line> clientModelList = repair_lineClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Irepair_line clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Repair_line>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据库存位置查询
     */
    @Override
    public Page<Repair_line> selectByOdoo_location_dest(Integer id){
        Repair_lineSearchContext context = new Repair_lineSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Repair_line> list = new ArrayList<Repair_line>();
        Page<cn.ibizlab.odoo.core.client.model.Irepair_line> clientModelList = repair_lineClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Irepair_line clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Repair_line>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据库存位置查询
     */
    @Override
    public Page<Repair_line> selectByOdoo_location(Integer id){
        Repair_lineSearchContext context = new Repair_lineSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Repair_line> list = new ArrayList<Repair_line>();
        Page<cn.ibizlab.odoo.core.client.model.Irepair_line> clientModelList = repair_lineClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Irepair_line clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Repair_line>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据库存移动查询
     */
    @Override
    public Page<Repair_line> selectByOdoo_move(Integer id){
        Repair_lineSearchContext context = new Repair_lineSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Repair_line> list = new ArrayList<Repair_line>();
        Page<cn.ibizlab.odoo.core.client.model.Irepair_line> clientModelList = repair_lineClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Irepair_line clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Repair_line>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据批次/序列号查询
     */
    @Override
    public Page<Repair_line> selectByOdoo_lot(Integer id){
        Repair_lineSearchContext context = new Repair_lineSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Repair_line> list = new ArrayList<Repair_line>();
        Page<cn.ibizlab.odoo.core.client.model.Irepair_line> clientModelList = repair_lineClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Irepair_line clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Repair_line>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据产品计量单位查询
     */
    @Override
    public Page<Repair_line> selectByOdoo_product_uom(Integer id){
        Repair_lineSearchContext context = new Repair_lineSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Repair_line> list = new ArrayList<Repair_line>();
        Page<cn.ibizlab.odoo.core.client.model.Irepair_line> clientModelList = repair_lineClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Irepair_line clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Repair_line>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Irepair_line convert2Model(Repair_line domain , Irepair_line model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = repair_lineClientService.createModel() ;
        }

         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getNameDirtyFlag())
            model.setName(domain.getName());
         if(domain.getProduct_uom_qtyDirtyFlag())
            model.setProduct_uom_qty(domain.getProduct_uom_qty());
         if(domain.getInvoicedDirtyFlag())
            model.setInvoiced(domain.getInvoiced());
         if(domain.getStateDirtyFlag())
            model.setState(domain.getState());
         if(domain.getTypeDirtyFlag())
            model.setType(domain.getType());
         if(domain.getPrice_unitDirtyFlag())
            model.setPrice_unit(domain.getPrice_unit());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getTax_idDirtyFlag())
            model.setTax_id(domain.getTax_id());
         if(domain.getPrice_subtotalDirtyFlag())
            model.setPrice_subtotal(domain.getPrice_subtotal());
         if(domain.getLocation_dest_id_textDirtyFlag())
            model.setLocation_dest_id_text(domain.getLocation_dest_id_text());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getMove_id_textDirtyFlag())
            model.setMove_id_text(domain.getMove_id_text());
         if(domain.getLocation_id_textDirtyFlag())
            model.setLocation_id_text(domain.getLocation_id_text());
         if(domain.getLot_id_textDirtyFlag())
            model.setLot_id_text(domain.getLot_id_text());
         if(domain.getProduct_uom_textDirtyFlag())
            model.setProduct_uom_text(domain.getProduct_uom_text());
         if(domain.getInvoice_line_id_textDirtyFlag())
            model.setInvoice_line_id_text(domain.getInvoice_line_id_text());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getProduct_id_textDirtyFlag())
            model.setProduct_id_text(domain.getProduct_id_text());
         if(domain.getRepair_id_textDirtyFlag())
            model.setRepair_id_text(domain.getRepair_id_text());
         if(domain.getLot_idDirtyFlag())
            model.setLot_id(domain.getLot_id());
         if(domain.getRepair_idDirtyFlag())
            model.setRepair_id(domain.getRepair_id());
         if(domain.getMove_idDirtyFlag())
            model.setMove_id(domain.getMove_id());
         if(domain.getInvoice_line_idDirtyFlag())
            model.setInvoice_line_id(domain.getInvoice_line_id());
         if(domain.getProduct_idDirtyFlag())
            model.setProduct_id(domain.getProduct_id());
         if(domain.getLocation_idDirtyFlag())
            model.setLocation_id(domain.getLocation_id());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getLocation_dest_idDirtyFlag())
            model.setLocation_dest_id(domain.getLocation_dest_id());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
         if(domain.getProduct_uomDirtyFlag())
            model.setProduct_uom(domain.getProduct_uom());
        return model ;
    }

    public Repair_line convert2Domain( Irepair_line model ,Repair_line domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Repair_line();
        }

        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getNameDirtyFlag())
            domain.setName(model.getName());
        if(model.getProduct_uom_qtyDirtyFlag())
            domain.setProduct_uom_qty(model.getProduct_uom_qty());
        if(model.getInvoicedDirtyFlag())
            domain.setInvoiced(model.getInvoiced());
        if(model.getStateDirtyFlag())
            domain.setState(model.getState());
        if(model.getTypeDirtyFlag())
            domain.setType(model.getType());
        if(model.getPrice_unitDirtyFlag())
            domain.setPrice_unit(model.getPrice_unit());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getTax_idDirtyFlag())
            domain.setTax_id(model.getTax_id());
        if(model.getPrice_subtotalDirtyFlag())
            domain.setPrice_subtotal(model.getPrice_subtotal());
        if(model.getLocation_dest_id_textDirtyFlag())
            domain.setLocation_dest_id_text(model.getLocation_dest_id_text());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getMove_id_textDirtyFlag())
            domain.setMove_id_text(model.getMove_id_text());
        if(model.getLocation_id_textDirtyFlag())
            domain.setLocation_id_text(model.getLocation_id_text());
        if(model.getLot_id_textDirtyFlag())
            domain.setLot_id_text(model.getLot_id_text());
        if(model.getProduct_uom_textDirtyFlag())
            domain.setProduct_uom_text(model.getProduct_uom_text());
        if(model.getInvoice_line_id_textDirtyFlag())
            domain.setInvoice_line_id_text(model.getInvoice_line_id_text());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getProduct_id_textDirtyFlag())
            domain.setProduct_id_text(model.getProduct_id_text());
        if(model.getRepair_id_textDirtyFlag())
            domain.setRepair_id_text(model.getRepair_id_text());
        if(model.getLot_idDirtyFlag())
            domain.setLot_id(model.getLot_id());
        if(model.getRepair_idDirtyFlag())
            domain.setRepair_id(model.getRepair_id());
        if(model.getMove_idDirtyFlag())
            domain.setMove_id(model.getMove_id());
        if(model.getInvoice_line_idDirtyFlag())
            domain.setInvoice_line_id(model.getInvoice_line_id());
        if(model.getProduct_idDirtyFlag())
            domain.setProduct_id(model.getProduct_id());
        if(model.getLocation_idDirtyFlag())
            domain.setLocation_id(model.getLocation_id());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getLocation_dest_idDirtyFlag())
            domain.setLocation_dest_id(model.getLocation_dest_id());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        if(model.getProduct_uomDirtyFlag())
            domain.setProduct_uom(model.getProduct_uom());
        return domain ;
    }


}
